import axios from "axios";
import qs from 'qs'
import message from '@arco-design/web-vue/es/message'
const service = axios.create({
    baseURL: 'http://localhost:8101/',
    timeout: 15000
})
//前置请求
axios.interceptors.request.use(
    function (config) {
        //携带token
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);
//后置请求
axios.interceptors.response.use(
    function (response) {
        return response;
    },
    function (error) {
        return Promise.reject(error);
    }
);

//请求方法
const http = {
    post(url:any, params:any) {
        return service.post(url , params, {
            transformRequest: [(params) => {
                return JSON.stringify(params)
            }],
            headers: {
                'Content-Type': 'application/json'
            }
        })
    },
    put(url:any, params:any) {
        return service.put(url, params, {
            transformRequest: [(params) => {
                return JSON.stringify(params)
            }],
            headers: {
                'Content-Type': 'application/json'
            }
        })
    },
    get(url:any, params:any) {
        return service.get(url, {
            params: params,
            paramsSerializer: (params) => {
                return qs.stringify(params)
            }
        })
    },
    getRestApi(url:any, params:any) {
        let _params
        if (Object.is(params, undefined || null)) {
            _params = ''
        } else {
            _params = '/'
            for (const key in params) {
                console.log(key)
                console.log(params[key])
                if (params.hasOwnProperty(key) && params[key] !== null && params[key]
                    !== '') {
                    _params += `${params[key]}/`
                }
            }
            _params = _params.substr(0, _params.length - 1)
        }
        console.log(_params)
        if (_params) {
            return service.get(`${url}${_params}`)
        } else {
            return service.get(url)
        }
    },
    delete(url:any, params:any) {
        let _params
        if (Object.is(params, undefined || null)) {
            _params = ''
        } else {
            _params = '/'
            for (const key in params) {
                // eslint-disable-next-line no-prototype-builtins
                if (params.hasOwnProperty(key) && params[key] !== null && params[key]
                    !== '') {
                    _params += `${params[key]}/`
                }
            }
            _params = _params.substr(0, _params.length - 1)
        }
        if (_params) {
            return service.delete(`${url}${_params}`).catch(err => {
                message.error(err.msg)
                return Promise.reject(err)
            })
        } else {
            return service.delete(url).catch(err => {
                message.error(err.msg)
                return Promise.reject(err)
            })
        }
    },
    upload(url:any, params:any) {
        return service.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    },
    login(url:any, params:any) {
        return service.post(url, params, {
            transformRequest: [(params) => {
                return qs.stringify(params)
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });
    },
}
export default http;